#include "database.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QFile>

QSqlDatabase Database::db;

bool Database::connect() {

    db = QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName("project.db");

    if (!db.open()) {
        qDebug() << "无法连接数据库：" << db.lastError();
        return false;
    }

    qDebug() << "Data base open successfully";

    QString createTableSql = R"(
            CREATE TABLE IF NOT EXISTS projects (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                projectName TEXT NOT NULL,
                priority TEXT,
                status TEXT,
                businessLeader TEXT,
                softWareLeader TEXT,
                startTime DATE DEFAULT '2025-07-15',
                consumeTime INTEGER,
                endTime DATE DEFAULT '2025-07-15',
                requirementsDocumentation TEXT,
                bom TEXT,
                softwareRecords TEXT
            )
        )";

    QSqlQuery query;
    if (!query.exec(createTableSql)) {
        qDebug() << "创建表失败：" << query.lastError();
        return false;
    }

    return true;
}
